//
//  myFansVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class myFansVC: UIViewController, UITableViewDataSource, UITableViewDelegate, MyFansCellDelegate {
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var concernNum: UILabel!
    @IBOutlet weak var headerView: UIView!
    var modelList: myConcernInfo?{
        didSet{
            self.tableView.reloadData()
            
        }
    }
    var msg: regiestInfo?
    var id: String?
    var guanzhu: Bool?
    override func viewDidLoad() {
        super.viewDidLoad()
       self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.tableHeaderView = headerView
        let cellNib = UINib(nibName: "MyFansCell", bundle: nil)
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "MyFansCell")
        self.getMyConcernInfo()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
       
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if modelList?.users.count == nil{
            return 0
        }else{
            return (modelList?.users.count)!
        }
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 55
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("MyFansCell", forIndexPath: indexPath) as! MyFansCell
        
        let model = modelList?.users[indexPath.row]
        cell.model = model
        
        cell.idLbl.text = model?.name
        
        cell.delegate = self
        cell.iv.sd_setImageWithURL(NSURL(string: model!.timg), placeholderImage: UIImage(named: "placeholder"))
        
        if model?.isguanzhu == true{
           cell.AddBtn.setImage(UIImage(named: "guanzhu_selected"), forState: UIControlState.Normal)
        }else{
             cell.AddBtn.setImage(UIImage(named: "guanzhu"), forState: UIControlState.Normal)
        }
        cell.signLbl.text = model?.signature
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        let sb = UIStoryboard.init(name: "NewsList", bundle: nil)
        
        let vc = sb.instantiateViewControllerWithIdentifier("UserHomeTableViewController") as! UserHomeTableViewController
        
        vc.userId = Int((self.modelList?.users[indexPath.row].id)!)
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    //MARK:NETWORKING
    //MARK:MyFansCellDelegate
    func addFocus(id: String, guanzhu: Bool, isme: Bool){
        if guanzhu == true{
            DataTool.followOrCancel(id, isguanzhu: false, completionHandler: { (newModel) in
                guard newModel != nil else {
                    return
                }
                self.msg = newModel
                if self.msg?.msg == "取消关注成功！"{
                    self.getMyConcernInfo()
                    SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                    
                }else{
                    SVProgressHUD.showErrorWithStatus(self.msg?.msg)
                    
                }
                
                
            })
        }else{
            DataTool.followOrCancel(id, isguanzhu: true, completionHandler: { (newModel) in
                guard newModel != nil else {
                    return
                }
                self.msg = newModel
                if self.msg?.msg == "关注成功！"{
                    self.getMyConcernInfo()
                    SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                    
                }else{
                    SVProgressHUD.showErrorWithStatus(self.msg?.msg)
                    
                }
                
                
            })

        }
        
    }

    func getMyConcernInfo(){
        DataTool.myFensi({ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            
            print(newDataes)
            self.modelList = newDataes
            if self.modelList?.guanzhunum == ""{
                self.concernNum.text = "还没有关注"
            }else{
                self.concernNum.text = String(format: "被%@个人关注",(self.modelList?.fensinum)!)
            }
            
        })
        
        
    }
}
